package com.laikan.reader.core.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.R;
import com.laikan.reader.core.bean.ChapterBean;
import com.laikan.reader.core.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BookChapterAdapter extends LaikanBaseAdapter<ChapterBean> {

    private int pos;
    private File Filejson;
    private List<String> mFileList = new ArrayList<String>();
    private Resources resources;
    private String book_id;
    private ArrayList<ChapterBean> chapterData = new ArrayList<ChapterBean>();
    private boolean isInitChapterData = false;

    public BookChapterAdapter(Context context, ArrayList<ChapterBean> datas, String book_id) {
        super(context, datas);
        this.book_id = book_id;
        if (FileUtil.isHaveSDCard()) {
            Filejson = Environment.getExternalStorageDirectory();

        }else {
            Filejson = Environment.getDataDirectory();
        }
        resources = mContext.getResources();
    }

    private int getAllPos(String chapterId) {
        for (int i = 0; i < datas.size(); i++) {
            if (chapterId.equals(datas.get(i).getId())) {
                return i;
            }
        }
        return 0;
    }

    //获取已下载章节id
    public List<String> getFile(File file) {
        File[] fileArray = file.listFiles();
        try {
            for (File f : fileArray) {
                if (f.isFile()) {
                    mFileList.add(f.getName());
                }else {
                    getFile(f);
                }
            }
        }catch (Exception e) {
            // TODO: handle exception
        }

        return mFileList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.lk_chapter_item, null);
            holder.chapter_name = (TextView)convertView.findViewById(R.id.chapter_name);
            holder.chapter_name1 = (TextView)convertView.findViewById(R.id.chapter_name1);
            holder.chapter_name2 = (TextView)convertView.findViewById(R.id.chapter_name2);
            holder.vip_img = (ImageView)convertView.findViewById(R.id.vip_img);
            holder.vip_img2 = (ImageView)convertView.findViewById(R.id.vip_img2);
            holder.du_item = (RelativeLayout)convertView.findViewById(R.id.du_item);
            holder.juan_item = (RelativeLayout)convertView.findViewById(R.id.juan_item);
            holder.zhang_item = (RelativeLayout)convertView.findViewById(R.id.zhang_item);
            holder.text_free = (TextView)convertView.findViewById(R.id.text_free);
            holder.text_free1 = (TextView)convertView.findViewById(R.id.text_free1);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        File f = new File(Filejson.getPath() + "/LaikanReader/book/" + book_id);
        getFile(f);
        if (!isInitChapterData) {
            for (int i = 0; i < datas.size(); i++) {
                if (!datas.get(i).isVolume()) {
                    chapterData.add(datas.get(i));
                }
            }
            isInitChapterData = true;
        }

        int pos1 = SPUtil.getInt(book_id + "chaptep_rec", 1);
        if (pos1 < 0) {
            pos1 = 1;
        }
        if (pos1 > chapterData.size()) {
            pos1 = chapterData.size()-1;
        }
        if(chapterData.size()==1){
            pos = getAllPos(chapterData.get(0).getId());
        }else{
            pos = getAllPos(chapterData.get(pos1).getId());
        }
//        if (position < 0) {
//            position = 1;
//        }
        if (mFileList.contains(datas.get(position).getId())) {

            holder.chapter_name.setTextColor(resources.getColor(R.color.C_333333));
            holder.chapter_name1.setTextColor(resources.getColor(R.color.C_333333));
            if (pos == position) {
                holder.chapter_name2.setTextColor(resources.getColor(R.color.C_66ccdd));
                holder.text_free1.setTextColor(resources.getColor(R.color.C_66ccdd));
            }else {
                holder.chapter_name2.setTextColor(resources.getColor(R.color.C_333333));
                holder.text_free1.setTextColor(resources.getColor(R.color.C_333333));
            }
            holder.text_free.setTextColor(resources.getColor(R.color.C_333333));

        }else {
            holder.chapter_name.setTextColor(resources.getColor(R.color.C_999999));
            holder.chapter_name1.setTextColor(resources.getColor(R.color.C_999999));
            holder.chapter_name2.setTextColor(resources.getColor(R.color.C_999999));
            holder.text_free.setTextColor(resources.getColor(R.color.C_999999));
            holder.text_free1.setTextColor(resources.getColor(R.color.C_999999));
        }
        if (pos == position) {
            holder.du_item.setVisibility(View.VISIBLE);
            holder.juan_item.setVisibility(View.GONE);
            holder.zhang_item.setVisibility(View.GONE);

            holder.chapter_name2.setText(datas.get(position).getName());
            if (datas.get(position).isFree()) {
                holder.vip_img2.setVisibility(View.GONE);
                holder.text_free.setVisibility(View.VISIBLE);
                holder.text_free1.setVisibility(View.VISIBLE);
            }else if (!datas.get(position).isFree()) {
                if (!datas.get(position).isVolume()) {
                    //					holder.vip_img2.setVisibility(View.VISIBLE);
                    holder.text_free.setVisibility(View.GONE);
                    holder.text_free1.setVisibility(View.GONE);
                }
            }
        }else {
            if (datas.get(position).isVolume()) {
                holder.du_item.setVisibility(View.GONE);
                holder.juan_item.setVisibility(View.VISIBLE);
                holder.zhang_item.setVisibility(View.GONE);
                holder.vip_img2.setVisibility(View.GONE);
                holder.chapter_name1.setText(datas.get(position).getName());
            }else if (!datas.get(position).isVolume()) {
                holder.du_item.setVisibility(View.GONE);
                holder.juan_item.setVisibility(View.GONE);
                holder.zhang_item.setVisibility(View.VISIBLE);
                holder.chapter_name.setText(datas.get(position).getName());
                if (datas.get(position).isFree()) {
                    holder.vip_img.setVisibility(View.GONE);
                    holder.text_free.setVisibility(View.VISIBLE);
                    holder.text_free1.setVisibility(View.VISIBLE);
                }else if (!datas.get(position).isFree()) {
                    //					holder.vip_img.setVisibility(View.VISIBLE);
                    holder.text_free.setVisibility(View.GONE);
                    holder.text_free1.setVisibility(View.GONE);
                }
            }
        }
        return convertView;
    }

    private class ViewHolder {

        public TextView chapter_name, chapter_name2, chapter_name1, text_free, text_free1;
        public ImageView vip_img, vip_img2;
        public RelativeLayout du_item, juan_item, zhang_item;
    }

}
